const { network } = chrome.devtools;

export default class NetworkListener {

  listners = [];

  constructor(listener) {
    if (listener) {
      listener.push(listener);
    }
    network.onRequestFinished.addListener((fullRequestAndResponse) => {
      
      const { _resourceType } = fullRequestAndResponse;

      if (_resourceType !== "xhr" && _resourceType !== "fetch") {
        return;
      }

      this.listners.forEach(listener => {
        listener.cb(fullRequestAndResponse);
      });
    });
  }

  addListener(listener) {
    this.listners.push(listener);
  }

  removeListener(id) {
    this.listners.splice(this.listners.indexOf(id));
  }
}